﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCRUM.Core.BusinessObjects;
using SCRUM.Core.DAO;

/// <summary>
/// Handles operations on Srpint, create, show detail, edit, delete
/// </summary>
public partial class SprintDetails : HierarchicalObjectDetails<Sprint,Release>
{

    /// <summary>
    /// Loads the extended inputs from.
    /// </summary>
    protected override IList<ScrumInput> LoadExtendedInputs()
    {
        return this.LoadExtendedInputsFrom(PanelItemDetails);
    }

    /// <summary>
    /// Loads the available superior items (Releases).
    /// </summary>
    protected override void LoadAvailableSuperiorItems()
    {
        AvailableSuperiorItems = DAORelease.GetAllReleases();
    }

    /// <summary>
    /// Displays the srint.
    /// </summary>
    /// <param name="sprint">The sprint to be displayed.</param>
    public override void DisplayItem(Sprint sprint)
    {
        this.Visible = true;

        AllReleases.DataSource = AvailableSuperiorItems;
        AllReleases.DataBind();

        bool exists = sprint != null;

        this.Header.LastChange =        exists ? sprint.LastChange  : DateTime.MinValue;
        this.Name.Text =                exists ? sprint.Name        : "";
        this.Description.Text =         exists ? sprint.Description : "";
        this.StartDate.SeledtedDate =   exists ? sprint.FromDate    : DateTime.Now;
        this.EndDate.SeledtedDate =     exists ? sprint.ToDate      : DateTime.Now + Sprint.DefaultLength;
        this.States.SelectedState =     exists ? sprint.State       : State.DefaultState(State.StateTypes.Sprint);
        this.Progress.Progress =        exists ? sprint.Progress    : 0;
        this.Estimation.Value =         exists ? sprint.Estimation  : 0;
        int superiorItemID =            exists ? sprint.SuperiorItemID : this.SuperiorItemID;
        if (superiorItemID != -1)
        {
            this.AllReleases.SelectedValue = superiorItemID.ToString();
        }
        
        // enabled
        this.States.Enabled = exists;
        this.AllReleases.Enabled = exists;

        // visibility
        this.Progress.Visible = exists;
        //this.Estimation.Visible = exists;    
    }

    /// <summary>
    /// Creates new sprint
    /// </summary>
    /// <returns>Created sprint</returns>
    public override Sprint CreateItem()
    {
        int releaseID = Int32.Parse(AllReleases.SelectedValue);
        Sprint sprint = new Sprint(
            0,
            this.Name.Text,
            this.Description.Text,
            this.States.SelectedState.ID,
            this.StartDate.SeledtedDate.Value,
            this.EndDate.SeledtedDate.Value,
            this.Estimation.Value,
            0, 
            DateTime.Now,
            0,
            0,
            0,
            releaseID
        );
        Sprint newSprint = DAOSprint.CreateNewSprint(sprint, releaseID);
        SelectedItem = newSprint;
        return newSprint;
    }

    /// <summary>
    /// Edits sprint according to data in Sprint detail panel
    /// </summary>
    /// <param name="sprOld">Sprint to update</param>
    public override Sprint EditItem(int sprOldId)
    {
        int releaseID = Int32.Parse(AllReleases.SelectedValue);
        Sprint sprint = new Sprint(
            sprOldId,
            this.Name.Text,
            this.Description.Text,
            this.States.SelectedState.ID,
            this.StartDate.SeledtedDate.Value,
            this.EndDate.SeledtedDate.Value,
            this.Estimation.Value,
            0, 
            DateTime.Now,
            0,
            0,
            0,
            releaseID
        );
        DAOSprint.UpdateSprint(sprint, releaseID);
        SelectedItem = sprint;
        //DisplayItem(sprint);
        return sprint;
    }

    public int GetInRelease()
    {
        return System.Convert.ToInt16(AllReleases.SelectedValue);
    }

    /// <summary>
    /// Deletes Sprint
    /// </summary>
    /// <param name="sprint">Sprint to delete</param>
    public override void DeleteItem(Sprint sprint)
    {
        DAOSprint.DeleteSprint(sprint);
        this.Visible = false;
    }

    /// <summary>
    /// Validates the input.
    /// </summary>
    /// <returns>Validation results</returns>
    public override bool ValidateInput()
    {        
        return true;
    }

    protected override Panel panelWithControls { get { return this.PanelItemDetails; } }
}
